#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

string findMaxNumberLessThanN(int n, vector<int>& nums) {
   if(nums.empty())
    return "0";
   
    sort(nums.begin(), nums.end());
    string s = to_string(n);
    int len = s.size();

    vector<int> ans(len,-1);
    vector<int> index(len,-1);
    bool good = true;
    int i = 0;

    while(i < len){
        if(!good){
            ans[i] = nums.back();
            index[i] = nums.size() - 1;
            i++;
        } else{
            int num = s[i] - '0';
            int mmax = num;
            if(i == len - 1)
                mmax = num -1;

            auto it = upper_bound(nums.begin(), nums.end(), mmax);
            int keyi = -1;
            if(it != nums.begin())
                keyi = (it - nums.begin()) - 1;

            if(keyi >= 0){
                ans[i] = nums[keyi];
                index[i] = keyi;
                if(nums[keyi] < num)
                    good = false;
                i++;
            } else{
                bool back = false;
                for(int j = i-1;j>=0;j--){
                    if(index[j] > 0){
                        int newi = index[j]-1;
                        ans[j] = nums[newi];
                        index[j] = newi;
                        for(int k = j + 1;k<len;k++)
                            ans[k] = nums.back();
                        back = true;
                        break;
                    }
                }
                if(back)
                    break;
                else{
                    if(len == 1)
                        ans = vector<int>();
                    else    
                        ans = vector<int>(len -1, nums.back());
                    break;
                }
            }
        }
    }

    string result = "";
    bool zero = true;
    for(int x : ans){
        if(x == 0 && zero)
            continue;
        if(x != 0)
            zero = false;
        result += x+'0';
    }
    if(result.empty())
        result = "0";

    return result;
}


int main() {
    while (true) {
        int n;
        vector<int> a;
        string input;

        cout << "请输入数字n（输入q退出）: ";
        cin >> input;
        if (input == "q" || input == "Q") {
            break;
        }
        try {
            n = stoi(input);
        } catch (...) {
            cout << "输入无效，请重新输入数字n。" << endl;
            continue;
        }

        cout << "请输入一组数字a，以空格分隔（例如：2 4 9）: ";
        cin.ignore(); // 清除之前的换行符
        getline(cin, input);
        istringstream iss(input);
        int num;
        while (iss >> num) {
            a.push_back(num);
        }

        if (a.empty()) {
            cout << "数字组a不能为空，请重新输入。" << endl;
            continue;
        }

        cout << "结果为: " << findMaxNumberLessThanN(n, a) << endl;
        a.clear(); // 清空a以备下次输入
    }

    return 0;
}